﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace TFSTools.TestManagement.TestCaseReview
{
    public static class Formatter
    {
        public static void Format(TextBlock source, string value)
        {
            List<Run> afterBold = new List<Run>();

            foreach (string sp in Regex.Split(value, "</b>"))
            {
                string[] tempBold = Regex.Split(sp, "<b>");
                Run myRun = new Run();
                myRun.Text = tempBold[0];
                afterBold.Add(myRun);
                if (tempBold.Length > 1)
                {
                    Run myBoldRun = new Run();
                    myBoldRun.Text = tempBold[1];
                    myBoldRun.FontWeight = FontWeights.ExtraBold;
                    afterBold.Add(myBoldRun);
                }
            }

            List<Run> afterItalics = new List<Run>();

            foreach (Run r in afterBold)
            {
                foreach (string sp in Regex.Split(r.Text, "</i>"))
                {
                    string[] tempItalics = Regex.Split(sp, "<i>");
                    Run myRun = new Run();
                    myRun.Text = tempItalics[0];
                    myRun.FontWeight = r.FontWeight;
                    afterItalics.Add(myRun);
                    if (tempItalics.Length > 1)
                    {
                        Run myItalicRun = new Run();
                        myItalicRun.Text = tempItalics[1];
                        myItalicRun.FontWeight = r.FontWeight;
                        myItalicRun.FontStyle = FontStyles.Italic;
                        afterItalics.Add(myItalicRun);
                    }
                }
            }

            List<Run> afterUnderline = new List<Run>();

            foreach (Run r in afterItalics)
            {
                foreach (string sp in Regex.Split(r.Text, "</u>"))
                {
                    string[] tempUnderline = Regex.Split(sp, "<u>");
                    Run myRun = new Run();
                    myRun.Text = tempUnderline[0];
                    myRun.FontWeight = r.FontWeight;
                    myRun.FontStyle = r.FontStyle;
                    afterUnderline.Add(myRun);
                    if (tempUnderline.Length > 1)
                    {
                        Run myUnderlinedRun = new Run();
                        myUnderlinedRun.Text = tempUnderline[1];
                        myUnderlinedRun.FontWeight = r.FontWeight;
                        myUnderlinedRun.FontStyle = r.FontStyle;
                        myUnderlinedRun.TextDecorations = TextDecorations.Underline;
                        afterUnderline.Add(myUnderlinedRun);
                    }
                }
            }

            foreach (Run r in afterUnderline)
            {
                source.Inlines.Add(r);
            }
        }
    }
}
